package com.xuxueli.applyModules.utils;
import com.xuxueli.applyModules.utils.testcharts.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author linglong.weng
 * @date 2020/3/4
 */
@Service
public class RedisUtils {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime, TimeUnit timeUnit) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, timeUnit);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param keys
     */
    public void remove(List<String> keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 删除key
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 批量删除
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 哈希添加
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void putHash(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }

    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */
    public Object getHash(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void addList(String k, Object v) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(k, v);
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void addList(String k, Object v, long timeout, TimeUnit unit) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        if(ObjectUtils.isEmpty(v)){
            v = "";
        }
        list.rightPush(k, v);
        redisTemplate.expire(k, timeout, unit);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<Object> getList(String k, long l, long l1) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k, l, l1);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void addSet(String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }

    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<Object> getSet(String key) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param scoure
     */
    public void zAdd(String key, Object value, double scoure) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key, value, scoure);
    }

    /**
     * 有序集合获取
     *
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }

    /**
     * 指定下标取值
     *
     * @param key
     * @param index
     * @return
     */
    public String getByIndex(String key, int index) {
        //获取指定下标对应的值 index,从0开始，有正负两套下标
        //[a,b,c,d] 下标有[0,1,2,3]和[0,-3,-2,-1];
        String value = redisTemplate.opsForList().index(key, index).toString();
        return value;
    }

    /**
     * 根据psno移除Redis的key值
     *
     * @author linglong.weng
     * @date 2020/3/4
     */
    public void removeKeyByPsno(String psno) {
        remove(psno);
    }

    /**
     * 切换redis数据库
     *
     * @author linglong.weng
     * @date 2020/4/29
     */
    public void changeDb(int dbIndex) {
//        RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
//        standaloneConfiguration.setDatabase(dbIndex);
//        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(standaloneConfiguration);
//        connectionFactory.afterPropertiesSet();
//        redisTemplate.setConnectionFactory(connectionFactory);
    }

    public static void main(String[] args) {
        File file = new File("E:\\data\\外源状态码批量上传模板.xlsx");
        List<Object[]> dataList = null;
        try {
            dataList = ExcelUtil.getData(new FileInputStream(file), "外源状态码批量上传模板.xlsx", 1, 1);
        } catch (IOException e) {
            System.out.println("asac");
        }
        Object[] rowData;
        for (int index = 0; index < dataList.size(); index++) {
            rowData = dataList.get(index);
            if (rowData != null) {
                System.out.println(rowData[0].toString());
                System.out.println(rowData[1].toString());
                System.out.println(rowData[2].toString());
                System.out.println(rowData[3].toString());
            }
        }
    }
}
